package com.bjpowernode.crm.settings.web.controller;

import com.alibaba.druid.stat.JdbcStatManagerFactoryBean;
import com.bjpowernode.crm.commons.constant.Constant;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:luosheng
 * @Date:2023/1/19 20:04
 * @Description:
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/settings/qx/user/tologin")
    public String tologin() {
        return "settings/qx/user/login";
    }

    @RequestMapping("/settings/qx/user/login")
    @ResponseBody
    public Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request, HttpServletResponse response, HttpSession session) {

        Map<String, Object> map = new HashMap<>();
        map.put("loginAct", loginAct);
        map.put("loginPwd", loginPwd);
        //调用service层方法，查询用户
        User user = userService.queryUserByLoginActAndPwd(map);

        ReturnObject returnObject = new ReturnObject();

        /**
         * 根据查询结果，生成相应信息
         */
        if (user == null) {
            /**
             * 登录失败,用户名胡总和密码错误
             */
            returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("账户密码错误");
        } else {
            /**
             * 进一步判断账号是否合法，判断登录时间是否过期
             */
            if (DateUtils.formateDateTime(new Date()).compareTo(user.getExpireTime()) > 0) {
                //这时候即使密码登录成功也登录失败，账号以过期
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("账号已经过期");
            } else if ("0".equals(user.getLockState())) {
                //登录失败,状态被锁定
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("登录状态被锁定");
            } else if (!request.getRemoteAddr().contains(user.getAllowIps())) {
                //登录失败，ip地址受到限制
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_FAIL);
                System.out.println(request.getRemoteAddr());
                returnObject.setMessage("ip地址受到限制");

            } else {
                //全部成功，则登录成功
                returnObject.setCode(Constant.RETURN_OBJECT_CODE_SUCCESS);

                session.setAttribute(Constant.SESSION_USER, user);

                //如果记住密码选项为true.则往外写cookie

                if ("true".equals(isRemPwd)) {
                    Cookie loginAct1 = new Cookie("loginAct", user.getLoginAct());
                    loginAct1.setMaxAge(10 * 24 * 60 * 60);
                    Cookie loginPwd1 = new Cookie("loginPwd", user.getLoginPwd());
                    loginPwd1.setMaxAge(10 * 24 * 60 * 60);
                    response.addCookie(loginAct1);
                    response.addCookie(loginPwd1);
                } else {
                    //如果下次登录它checkbo单选按钮给取消了，那么就删除掉cookie,也就删除记住账户,密码
                    //把没有过期cookie删除
                    //其实我们是不能删除cookie的，只是把cookie的过期时间设置为0
                    Cookie c1 = new Cookie("loginAct", "0321356");
                    //上面能够0321什么的随便输入的，不写不行(主要我们是设置loginAct的cookie的)
                    Cookie c2 = new Cookie("loginPwd", "0");

                    c1.setMaxAge(0);
                    c2.setMaxAge(0);

                    response.addCookie(c1);
                    response.addCookie(c2);
                }


            }

        }
        return returnObject;
    }


    @RequestMapping("/setting/qx/user/logout")
    public String logout(HttpServletResponse response, HttpSession session) {
        System.out.println("执行logout");
        Cookie c1 = new Cookie("loginAct", "0321356");
        //上面能够0321什么的随便输入的，不写不行(主要我们是设置loginAct的cookie的)
        Cookie c2 = new Cookie("loginPwd", "0");

        c1.setPath("/crm/settings/qx/user");
        c2.setPath("/crm/settings/qx/user");
        c1.setMaxAge(0);
        c2.setMaxAge(0);

        response.addCookie(c1);
        response.addCookie(c2);
//        //清除cookie对象
//        Cookie c3 = new Cookie("BAIDUID", null);
//        //上面能够0321什么的随便输入的，不写不行(主要我们是设置loginAct的cookie的)
//        Cookie c4 = new Cookie("ZFY", null);
//
//        c3.setMaxAge(0);
//        c4.setMaxAge(0);
//
//        response.addCookie(c3);
//        response.addCookie(c4);

//清除session对象
        session.invalidate();


        return "redirect:/";//借助springmvc来重定向，它的底层就相当于,response.sendRedirect("/项目名字/");
    }

}

